using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using ShoppingCenter.Backend.Api.Model;
using ShoppingCenter.Backend.Api.Params;
using ShoppingCenter.Backend.Api.Repository;
using ShoppingCenter.Backend.Api.Utils;

namespace ShoppingCenter.Backend.Api.Controllers
{
    // [Authorize]
    [ApiController]
    [Route("[controller]")]

    //支付控制器
    public class PayController : ControllerBase
    {
        private IConfiguration _configuration;

        //   支付方式
        IRepository<Pay> _payRepository;


        public PayController(IConfiguration configuration, IRepository<Pay> payRepository)
        {
            _configuration = configuration;
            _payRepository = payRepository;
        }

        //获取所有 支付方式
        public dynamic Get()
        {

            var currentPage = int.Parse(Request.Query["currentPage"][0]);
            var pageSize = int.Parse(Request.Query["pageSize"][0]);

            var pays = _payRepository.Table.ToList();

            // 分页
            var pay = pays.Skip((currentPage - 1) * pageSize).Take(pageSize); ;


            return new
            {
                Code = 1000,
                Data = new
                {
                    Data = pays,
                    Pager = new { currentPage, pageSize, totalSize = pays.Count() }
                },
                Msg = "获取所有支付方式成功"
            };
        }


        //添加支付方式
        [HttpPost]
        public dynamic Post(CUR_Pay newPay)
        {
            var payName = newPay.payName.Trim();
            var payUrl = FilesController.payUrl;

            if (string.IsNullOrEmpty(payName) || string.IsNullOrEmpty(payUrl))
            {

                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "支付或支付的二维码图不能为空"
                };
            }

            var addPay = new Pay
            {
                WeChat = payName,
                Alipay = payUrl,
            };


            //将支付或支付的二维码图插入表中
            _payRepository.Insert(addPay);

            return new
            {
                Code = 1000,
                Data = addPay,
                Msg = "添加支付方式成功"
            };

            //将结果实例化后输出

        }

        //修改支付方式
        [HttpPut("{id}")]
        public dynamic Put(int id, CUR_Pay updatePay)
        {
            var payName = updatePay.payName.Trim();
            var payUrl = FilesController.payUrl;


            if (string.IsNullOrEmpty(payName) || string.IsNullOrEmpty(payUrl))
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "支付或支付的二维码图不能为空"
                };
            }


            //通过Id获取要修改的支付方式
            var payUpdate = _payRepository.GetById(id);

            if (payUpdate == null)
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "你要更新的支付方式不存在"
                };
            }



            //将要修改的支付类型名称和图片重新赋给pay
            payUpdate.WeChat = updatePay.payName;
            payUpdate.Alipay = payUrl;


            //更新数据
            _payRepository.Update(payUpdate);

            return new
            {
                Code = 1000,
                Data = payUpdate,
                Msg = "更新支付方式成功"
            };

            // return JsonHelper.Serialize(res);

        }


        //根据id删除支付方式
        [HttpDelete("{id}")]
        public dynamic Dalete(int id)
        {

            var pay = _payRepository.GetById(id);

            if (pay == null)
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = string.Format("无法删除id为{0}的支付方式", id)
                };
            }

            _payRepository.Delete(id);

            return new
            {
                Code = 1000,
                Data = "",
                Msg = "删除当前支付方式成功"
            };
        }
    }
}